<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.1.1">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/avatar.jpg" color="#222">
  <meta name="msvalidate.01" content="4AF2328B2035748153A041B7EEF3D15C">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Serif+SC:300,300italic,400,400italic,700,700italic%7CLobster:300,300italic,400,400italic,700,700italic%7CPingFang+SC:300,300italic,400,400italic,700,700italic%7CRoboto+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ethanliu6.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":true,"sidebar":{"position":"left","display":"always","padding":12,"offset":18},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":true,"color":"#222","save":"manual"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"buttons","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":6,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="JavaSE基础——Day09 综合案例动漫美女拼图">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaSE基础——Day09 JavaSE基础——综合案例动漫美女拼图">
<meta property="og:url" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/index.html">
<meta property="og:site_name" content="&lt;font color&#x3D;&#39;white&#39; &gt;Ethan の Blog&lt;&#x2F;font&gt;">
<meta property="og:description" content="JavaSE基础——Day09 综合案例动漫美女拼图">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1640227177408.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1640228814196.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1640228689403.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1640228905184.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1636611659842.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1636613845486.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1636617395794.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1636622079054.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1636622825802.png">
<meta property="article:published_time" content="2024-03-23T16:00:00.000Z">
<meta property="article:modified_time" content="2024-04-26T05:12:45.318Z">
<meta property="article:author" content="Ethan Liu">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Java基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/imgs/1640227177408.png">


<link rel="canonical" href="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/","path":"2024/03/24/JavaNotes/JavaSE基础/day09动漫美女拼图/","title":"JavaSE基础——Day09 JavaSE基础——综合案例动漫美女拼图"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaSE基础——Day09 JavaSE基础——综合案例动漫美女拼图 | <font color='white' >Ethan の Blog</font></title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title"><font color='white' >Ethan の Blog</font></p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description"><font color='#FDABC1' size=3>清醒的停留，胜过——盲目的前行</font></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-关于我"><a href="/aboutme/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">18</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">14</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">53</span></a></li><li class="menu-item menu-item-love"><a href="/categories/Love/" rel="section"><i class="fas fa-heart fa-fw"></i>Love</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%A7%E6%89%BF"><span class="nav-text">1：继承</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%A7%E6%89%BF%E6%A6%82%E8%BF%B0"><span class="nav-text">1.1 继承概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%A7%E6%89%BF%E7%9A%84%E7%BB%83%E4%B9%A0"><span class="nav-text">1.2 继承的练习</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE"><span class="nav-text">2：动漫美女拼图</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A1%B9%E7%9B%AE%E6%BC%94%E7%A4%BA%E5%92%8C%E8%AF%BE%E7%A8%8B%E5%AE%89%E6%8E%92"><span class="nav-text">2.1 项目演示和课程安排</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE%E6%B8%B8%E6%88%8F%E5%AE%9E%E7%8E%B0"><span class="nav-text">2.2 动漫美女拼图游戏实现</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%AA%97%E4%BD%93%E7%BB%98%E5%88%B6"><span class="nav-text">2.2.1 窗体绘制</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%AA%97%E4%BD%93%E4%B8%8A%E7%BB%84%E4%BB%B6%E7%BB%98%E5%88%B6"><span class="nav-text">2.2.2 窗体上组件绘制</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9B%BE%E7%89%87%E6%89%93%E4%B9%B1"><span class="nav-text">2.2.3 图片打乱</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BA%AA%E5%BD%950%E5%8F%B7%E5%9B%BE%E7%89%87%E7%9A%84%E7%B4%A2%E5%BC%95"><span class="nav-text">2.2.4 纪录0号图片的索引</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%99%E6%8C%89%E9%92%AE%E6%B3%A8%E5%86%8C%E4%BA%8B%E4%BB%B6"><span class="nav-text">2.2.5 给按钮注册事件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%A7%BB%E5%8A%A8%E4%B8%9A%E5%8A%A1%E5%AE%9E%E7%8E%B0"><span class="nav-text">2.2.6 移动业务实现</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%8A%E7%A7%BB%E4%B8%9A%E5%8A%A1%E5%AE%9E%E7%8E%B0"><span class="nav-text">2.2.6.1 上移业务实现</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E7%A7%BB%E5%8A%A8%E4%B8%9A%E5%8A%A1%E5%AE%9E%E7%8E%B0"><span class="nav-text">2.2.6.2 其他移动业务实现</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%B1%82%E5%8A%A9%E4%B8%9A%E5%8A%A1%E5%AE%9E%E7%8E%B0"><span class="nav-text">2.2.7 求助业务实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%A7%BB%E5%8A%A8%E4%B8%9A%E5%8A%A1%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-text">2.2.8 移动业务的问题</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%87%8D%E7%BD%AE%E4%B8%9A%E5%8A%A1%E5%AE%9E%E7%8E%B0"><span class="nav-text">2.2.9 重置业务实现</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Ethan Liu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Ethan Liu</p>
  <div class="site-description" itemprop="description"><font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">53</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2V0aGFubGl1Ng==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ethanliu6"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmxpdWUyMzM2QGdtYWlsLmNvbQ==" title="E-Mail → mailto:liue2336@gmail.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vMTMyNzA5OTk3Nw==" title="BiliBili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;1327099977"><i class="fa-brands fa-bilibili fa-fw"></i>BiliBili</span>
      </span>
      <span class="links-of-author-item">
        <a href="/categories/Love/" title="Love → &#x2F;categories&#x2F;Love&#x2F;" rel="noopener me"><i class="fa fa-heart fa-fw"></i>Love</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Ethan Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="<font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="<font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font>">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JavaSE基础——Day09 JavaSE基础——综合案例动漫美女拼图 | <font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaSE基础——Day09 JavaSE基础——综合案例动漫美女拼图<span class="exturl post-edit-link" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni9ldGhhbmxpdTYuZ2l0aHViLmlvL3RyZWUvbWFzdGVyL19wb3N0cy9KYXZhTm90ZXMvSmF2YVNF5Z+656GAL2RheTA55Yqo5ryr576O5aWz5ou85Zu+Lm1k" title="编辑"><i class="fa fa-pen-nib"></i></span>
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-03-24 00:00:00" itemprop="dateCreated datePublished" datetime="2024-03-24T00:00:00+08:00">2024-03-24</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-04-26 13:12:45" itemprop="dateModified" datetime="2024-04-26T13:12:45+08:00">2024-04-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java%E5%85%A8%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">Java全栈</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    <a title="JavaSE基础——Day09 JavaSE基础——综合案例动漫美女拼图" href="/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/#SOHUCS" itemprop="discussionUrl">
      <span id="sourceId::a368e38d9c433de3d5cc55bd1f413229" class="cy_cmt_count" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>2.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>10 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><blockquote class="blockquote-center">
<p>JavaSE基础——Day09 综合案例<strong>动漫美女拼图</strong></p>

</blockquote>
<span id="more"></span>
<p><strong>今日目标：</strong></p>
<ul>
<li>能够知道继承的好处</li>
<li>能够理解使用继承改进窗体案例</li>
<li>能够知道动漫美女拼图游戏规则</li>
<li>能够完成动漫美女拼图游戏</li>
</ul>
<h2 id="继承">1：继承</h2>
<h3 id="继承概述">1.1 继承概述</h3>
<p>来，继续啊，为了讲解项目动漫拼图，这里我们先讲解一个小知识：继承概述。</p>
<p>通过这个名字呢，大家也发现了，这里我们仅仅是对继承做一个简单的讲解，后续呢，会深入的给大家再来讲解继承相关的知识。</p>
<p>首先，我们来说一下，什么是<strong>继承：</strong></p>
<ul>
<li>继承是面向对象三大特征之一(封装，继承和多态)</li>
<li>可以使得子类具有父类的属性和方法，还可以在子类中重新定义，追加属性和方法</li>
</ul>
<p>也就是说，通过继承，可以把父类中能够被访问到的成员变量和成员方法拿过来直接使用。</p>
<p>了解了什么是继承后，我们在来说一下，继承是如何实现的。</p>
<p>那继承是如何实现的呢？我们一起来看一下<strong>继承的格式：</strong></p>
<ul>
<li>格式：public class 子类名 extends 父类名 { }</li>
<li>范例：public class Zi
<font color="red"><strong>extends</strong></font> Fu { }</li>
<li>Fu：是父类，也被称为基类、超类</li>
<li>Zi：是子类，也被称为派生类</li>
</ul>
<p>在这里，Zi类和Fu类，通过extends就产生了继承关系。这样呢，Zi类就可以使用Fu类中的成员了。</p>
<p>在这里，Fu这个类，被称为是父类，也被称为基类、超类，Zi这个类：是子类，也被称为派生类</p>
<p>了解了继承是如何实现的，下面呢，我们再来举例说明一下，其实我们在前面已经使用过继承的知识了。</p>
<p>看这里，我们使用过GUI中这样的几个组件：</p>
<p>JLabel，JButton，JTextField，JTextArea</p>
<p>并且还使用过它们的一些方法，比如说：public void setBounds(int x, int
y, int width, int height)，这几个组件我们都使用过这样的一个方法。</p>
<p>大家想一下，如果我们在每个类中都定义这样的同体系的多个类中都使用的方法，有多个这样的组件，我们就要定义多少个这样的方法。</p>
<p>将来如果有新的组件，我们还是需要定义这样的方法，这样设计起来，我们程序的复用性是不是就太差了啊。</p>
<p>而且，这是Java给我们提供的API，Java大师们肯定不会有这么low的设计，</p>
<p>所以，为了提高代码的复用性，这里就采用了继承的思想，让一个类中定义这样的方法，所有继承该类的类就可以使用这个方法了。</p>
<p>来，打开帮助文档，我们一起去看一下：</p>
<figure>
<img data-src="imgs/1640227177408.png" alt="1640227177408">
<figcaption aria-hidden="true">1640227177408</figcaption>
</figure>
<p>其他的几个，大家打开文档自己去看，最终能够找到他们共同的父类：<strong>Component</strong>。</p>
<p>看完文档之后，回来总结一下，继承的好处之一是：<strong>提高了代码的复用性</strong></p>
<p>在这里，我们提到了什么是继承，继承的格式及继承的好处。</p>
<p>好了，关于继承概述我们就先讲到这里。</p>
<h3 id="继承的练习">1.2 继承的练习</h3>
<p>来，继续啊，下面呢我们来做一个继承的练习。</p>
<p>首先，我们来看一下需求：使用继承的方式，改写用户登录界面展示的案例</p>
<p>知道了，要做什么之后，下面呢，我们到IDEA中一边分析，一边实现：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserLoginFrame</span> <span class="keyword">extends</span> <span class="title class_">JFrame</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">UserLoginFrame</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//窗体初始化</span></span><br><span class="line">        initFrame();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//绘制窗体</span></span><br><span class="line">        paintView();</span><br><span class="line"></span><br><span class="line">        <span class="built_in">this</span>.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">paintView</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//显示用户名文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">usernameLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;用户名&quot;</span>);</span><br><span class="line">        usernameLable.setBounds(<span class="number">50</span>, <span class="number">50</span>, <span class="number">50</span>, <span class="number">20</span>);</span><br><span class="line">        <span class="built_in">this</span>.add(usernameLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//用户名输入框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">usernameField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        usernameField.setBounds(<span class="number">150</span>, <span class="number">50</span>, <span class="number">180</span>, <span class="number">20</span>);</span><br><span class="line">        <span class="built_in">this</span>.add(usernameField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示密码文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">passwordLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;密码&quot;</span>);</span><br><span class="line">        passwordLable.setBounds(<span class="number">50</span>, <span class="number">100</span>, <span class="number">50</span>, <span class="number">20</span>);</span><br><span class="line">        <span class="built_in">this</span>.add(passwordLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//密码输入框</span></span><br><span class="line">        <span class="type">JPasswordField</span> <span class="variable">passwordField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPasswordField</span>();</span><br><span class="line">        passwordField.setBounds(<span class="number">150</span>, <span class="number">100</span>, <span class="number">180</span>, <span class="number">20</span>);</span><br><span class="line">        <span class="built_in">this</span>.add(passwordField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//登录按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">loginButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;登录&quot;</span>);</span><br><span class="line">        loginButton.setBounds(<span class="number">50</span>, <span class="number">200</span>, <span class="number">280</span>, <span class="number">20</span>);</span><br><span class="line">        <span class="built_in">this</span>.add(loginButton);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">initFrame</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.setTitle(<span class="string">&quot;用户登录&quot;</span>);</span><br><span class="line">        <span class="built_in">this</span>.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        <span class="built_in">this</span>.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        <span class="built_in">this</span>.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        <span class="built_in">this</span>.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        <span class="built_in">this</span>.setLayout(<span class="literal">null</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">App</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">UserLoginFrame</span> <span class="variable">userLoginFrame</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">UserLoginFrame</span>();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>用继承改进后，代码看起来清晰多了，所以，如果我们做GUI开发的，在做窗体的时候，就会定义类继承自JFrame来使用。</p>
<p>好了，关于继承的练习我们就先讲到这里。</p>
<h2 id="动漫美女拼图">2：动漫美女拼图</h2>
<h3 id="项目演示和课程安排">2.1 项目演示和课程安排</h3>
<p>来，继续啊，通过前面所学知识，下面我们一起来实现动漫美女拼图这个项目。</p>
<p>为了让大家对这个项目有一定的了解，这里我们先来演示一下该项目：</p>
<p>来，打开准备好的Java文件，这里有两个类：</p>
<figure>
<img data-src="imgs/1640228814196.png" alt="1640228814196">
<figcaption aria-hidden="true">1640228814196</figcaption>
</figure>
<p>一个是动漫拼图窗体的类，一个是测试类。</p>
<p>右键运行测试类，我们看到了这样的一个界面。</p>
<figure>
<img data-src="imgs/1640228689403.png" alt="1640228689403">
<figcaption aria-hidden="true">1640228689403</figcaption>
</figure>
<p>大家可以通过按钮来玩这个游戏了。还可以点击求助按钮和重置按钮。</p>
<p>演示完动漫美女拼图项目，回来我们说一下这一块的课程安排：</p>
<figure>
<img data-src="imgs/1640228905184.png" alt="1640228905184">
<figcaption aria-hidden="true">1640228905184</figcaption>
</figure>
<p>首先，我们绘制游戏界面，把界面中要使用到的组件都给展示出来。</p>
<p>接着，我们讲解图片打乱，这样动漫拼图的界面就启动了。</p>
<p>然后，我们给按钮注册事件，这样我们就知道在哪里实现我们的功能了。</p>
<p>最后，我们来分别完成，移动业务，求助业务，重置业务，把整个项目给实现了</p>
<p>好了，动漫美女拼图项目的演示和课程安排我们就先讲到这里。</p>
<h3 id="动漫美女拼图游戏实现">2.2 动漫美女拼图游戏实现</h3>
<h4 id="窗体绘制">2.2.1 窗体绘制</h4>
<figure>
<img data-src="imgs/1636611659842.png" alt="1636611659842">
<figcaption aria-hidden="true">1636611659842</figcaption>
</figure>
<p><strong>分析思路：</strong></p>
<p>1：新建一个模块：itheima-picture-puzzle；在模块的src下新建一个包com.itheima</p>
<p>2：在com.itheima这个包下定义类：PictureFrame，继承自JFrame</p>
<p>3：在PictureFrame类中编写无参构造方法，在构造方法中调用两个方法：</p>
<p>​ 第一个方法：initFrame()，用于窗体的基本设置</p>
<p>​ 第二个方法：setVisible(true)，用于设置窗体可见</p>
<p>4：在initFrame()方法中编写代码，进行窗体的基本设置</p>
<p>​ 窗体大小</p>
<p>​ 窗体标题</p>
<p>​ 窗体居中</p>
<p>​ 窗体关闭时退出应用程序</p>
<p>​ 窗体位于其他窗口之上</p>
<p>​ 取消窗体默认布局</p>
<p>5：在com.itheima包下定义测试类：App；创建PictureFrame的对象进行测试</p>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PictureFrame</span> <span class="keyword">extends</span> <span class="title class_">JFrame</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">PictureFrame</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//用于窗体的基本设置</span></span><br><span class="line">        initFrame();</span><br><span class="line">        <span class="comment">//设置窗体可见</span></span><br><span class="line">        <span class="built_in">this</span>.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//用于窗体的基本设置</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">initFrame</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//窗体大小</span></span><br><span class="line">        <span class="built_in">this</span>.setSize(<span class="number">960</span>,<span class="number">565</span>);</span><br><span class="line">        <span class="comment">//窗体标题</span></span><br><span class="line">        <span class="built_in">this</span>.setTitle(<span class="string">&quot;动漫拼图&quot;</span>);</span><br><span class="line">        <span class="comment">//窗体居中</span></span><br><span class="line">        <span class="built_in">this</span>.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        <span class="comment">//窗体关闭时退出应用程序</span></span><br><span class="line">        <span class="built_in">this</span>.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        <span class="comment">//窗体位于其他窗口之上</span></span><br><span class="line">        <span class="built_in">this</span>.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        <span class="comment">//取消窗体默认布局</span></span><br><span class="line">        <span class="built_in">this</span>.setLayout(<span class="literal">null</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">App</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">PictureFrame</span> <span class="variable">pf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PictureFrame</span>();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="窗体上组件绘制">2.2.2 窗体上组件绘制</h4>
<figure>
<img data-src="imgs/1636613845486.png" alt="1636613845486">
<figcaption aria-hidden="true">1636613845486</figcaption>
</figure>
<p><strong>分析思路：</strong></p>
<p>1：定义方法，用于窗体上的组件绘制：paintView()</p>
<p>2：按照如下组件绘制</p>
<p>​ 标题图片</p>
<p>​ 面板图片，存储着将来要移动的图片</p>
<p>​ 参照图</p>
<p>​ 上按钮</p>
<p>​ 左按钮</p>
<p>​ 下按钮</p>
<p>​ 右按钮</p>
<p>​ 求助按钮</p>
<p>​ 重置按钮</p>
<p>3：在构造方法中调用paintView()方法</p>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//窗体上组件的绘制</span></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">paintView</span><span class="params">()</span> &#123;</span><br><span class="line">       <span class="comment">//标题图片</span></span><br><span class="line">       <span class="type">JLabel</span> <span class="variable">titleLabel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\title.png&quot;</span>));</span><br><span class="line">       titleLabel.setBounds(<span class="number">354</span>,<span class="number">27</span>,<span class="number">232</span>,<span class="number">57</span>);</span><br><span class="line">       <span class="built_in">this</span>.add(titleLabel);</span><br><span class="line"></span><br><span class="line">       <span class="comment">//定义一个二维数组，用来存储图片的编号</span></span><br><span class="line">       <span class="type">int</span>[][] datas = &#123;</span><br><span class="line">               &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>&#125;,</span><br><span class="line">               &#123;<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>&#125;,</span><br><span class="line">               &#123;<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>&#125;,</span><br><span class="line">               &#123;<span class="number">13</span>,<span class="number">14</span>,<span class="number">15</span>,<span class="number">16</span>&#125;</span><br><span class="line">       &#125;;</span><br><span class="line"></span><br><span class="line">       <span class="comment">//创建面板</span></span><br><span class="line">       <span class="type">JPanel</span> <span class="variable">imagePanel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPanel</span>();</span><br><span class="line">       imagePanel.setBounds(<span class="number">150</span>,<span class="number">114</span>,<span class="number">360</span>,<span class="number">360</span>);</span><br><span class="line">       imagePanel.setLayout(<span class="literal">null</span>);</span><br><span class="line">       <span class="comment">//遍历二维数组，得到图片编号</span></span><br><span class="line">       <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; datas.length; i++) &#123;</span><br><span class="line">           <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; datas[i].length; j++) &#123;</span><br><span class="line">               <span class="comment">//创建JLabel对象，加载图片资源</span></span><br><span class="line">               <span class="type">JLabel</span> <span class="variable">imageLabel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\&quot;</span>+datas[i][j]+<span class="string">&quot;.png&quot;</span>));</span><br><span class="line">               <span class="comment">//调整图片的位置</span></span><br><span class="line">               imageLabel.setBounds(j*<span class="number">90</span>,i*<span class="number">90</span>,<span class="number">90</span>,<span class="number">90</span>);</span><br><span class="line">               imagePanel.add(imageLabel);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="comment">//把面板添加到窗体上</span></span><br><span class="line">       <span class="built_in">this</span>.add(imagePanel);</span><br><span class="line"></span><br><span class="line">       <span class="comment">//动漫参照图</span></span><br><span class="line">       <span class="type">JLabel</span> <span class="variable">canZhaoTuLabel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\canzhaotu.png&quot;</span>));</span><br><span class="line">       canZhaoTuLabel.setBounds(<span class="number">574</span>,<span class="number">114</span>,<span class="number">122</span>,<span class="number">121</span>);</span><br><span class="line">       <span class="built_in">this</span>.add(canZhaoTuLabel);</span><br><span class="line"></span><br><span class="line">       <span class="comment">//上下左右，求助，重置按钮</span></span><br><span class="line">       <span class="type">JButton</span> <span class="variable">shangButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\shang.png&quot;</span>));</span><br><span class="line">       shangButton.setBounds(<span class="number">732</span>,<span class="number">265</span>,<span class="number">57</span>,<span class="number">57</span>);</span><br><span class="line">       <span class="built_in">this</span>.add(shangButton);</span><br><span class="line"></span><br><span class="line">       <span class="type">JButton</span> <span class="variable">zuoButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\zuo.png&quot;</span>));</span><br><span class="line">       zuoButton.setBounds(<span class="number">650</span>,<span class="number">347</span>,<span class="number">57</span>,<span class="number">57</span>);</span><br><span class="line">       <span class="built_in">this</span>.add(zuoButton);</span><br><span class="line"></span><br><span class="line">       <span class="type">JButton</span> <span class="variable">xiaButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\xia.png&quot;</span>));</span><br><span class="line">       xiaButton.setBounds(<span class="number">732</span>,<span class="number">347</span>,<span class="number">57</span>,<span class="number">57</span>);</span><br><span class="line">       <span class="built_in">this</span>.add(xiaButton);</span><br><span class="line"></span><br><span class="line">       <span class="type">JButton</span> <span class="variable">youButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\you.png&quot;</span>));</span><br><span class="line">       youButton.setBounds(<span class="number">813</span>,<span class="number">347</span>,<span class="number">57</span>,<span class="number">57</span>);</span><br><span class="line">       <span class="built_in">this</span>.add(youButton);</span><br><span class="line"></span><br><span class="line">       <span class="type">JButton</span> <span class="variable">qiuZhuButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\qiuzhu.png&quot;</span>));</span><br><span class="line">       qiuZhuButton.setBounds(<span class="number">626</span>,<span class="number">444</span>,<span class="number">108</span>,<span class="number">45</span>);</span><br><span class="line">       <span class="built_in">this</span>.add(qiuZhuButton);</span><br><span class="line"></span><br><span class="line">       <span class="type">JButton</span> <span class="variable">chongZhiButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\chongzhi.png&quot;</span>));</span><br><span class="line">       chongZhiButton.setBounds(<span class="number">786</span>,<span class="number">444</span>,<span class="number">108</span>,<span class="number">45</span>);</span><br><span class="line">       <span class="built_in">this</span>.add(chongZhiButton);</span><br><span class="line"></span><br><span class="line">       <span class="comment">//展示背景图</span></span><br><span class="line">       <span class="type">JLabel</span> <span class="variable">backgroundLabel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\background.png&quot;</span>));</span><br><span class="line">       backgroundLabel.setBounds(<span class="number">0</span>,<span class="number">0</span>,<span class="number">960</span>,<span class="number">530</span>);</span><br><span class="line">       <span class="built_in">this</span>.add(backgroundLabel);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">构造方法方法中调用绘图方法</span><br><span class="line">	<span class="keyword">public</span> <span class="title function_">PictureFrame</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//用于窗体的基本设置</span></span><br><span class="line">        initFrame();</span><br><span class="line">        <span class="comment">//窗体上组件的绘制</span></span><br><span class="line">        paintView();</span><br><span class="line">        <span class="comment">//设置窗体可见</span></span><br><span class="line">        <span class="built_in">this</span>.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="图片打乱">2.2.3 图片打乱</h4>
<figure>
<img data-src="imgs/1636617395794.png" alt="1636617395794">
<figcaption aria-hidden="true">1636617395794</figcaption>
</figure>
<p>图片打乱，其实就是二维数组元素打乱。</p>
<p><strong>注意事项：</strong></p>
<ul>
<li>由于在多个方法中使用同一个数组，故将二维数组的定义放置在成员位置</li>
<li>为了能够进行图片的移动，把16号图片用0号图片替换，当前15个移动到正确位置，显示正确的图片</li>
</ul>
<p><strong>分析思路：</strong></p>
<p>1：定义方法，用于二维数组元素打乱：initData()</p>
<p>2：创建Random对象</p>
<p>3：遍历存储图片编号的二维数组，得到每一个元素</p>
<p>4：产生两个随机索引，进行二维数组元素交换</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">int x = r.nextInt(datas.length);//行索引</span><br><span class="line">   int y = r.nextInt(datas[x].length);//列索引</span><br><span class="line"></span><br><span class="line">//元素交换</span><br><span class="line">   int temp = datas[i][j];</span><br><span class="line">   datas[i][j] = datas[x][y];</span><br><span class="line">   datas[x][y] = temp;</span><br></pre></td></tr></table></figure>
<p>5：在构造方法中调用initData()方法</p>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="type">int</span>[][] datas = &#123;</span><br><span class="line">        &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>&#125;,</span><br><span class="line">        &#123;<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>&#125;,</span><br><span class="line">        &#123;<span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>&#125;,</span><br><span class="line">        &#123;<span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>, <span class="number">0</span>&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//二维数组元素打乱</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">randomData</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; datas.length; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; datas[i].length; j++) &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> r.nextInt(datas.length);</span><br><span class="line">            <span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> r.nextInt(datas[i].length);</span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> datas[i][j];</span><br><span class="line">            datas[i][j] = datas[x][y];</span><br><span class="line">            datas[x][y] = temp;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="title function_">PictureFrame</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="comment">//用于窗体的基本设置</span></span><br><span class="line">    initFrame();</span><br><span class="line">    <span class="comment">//二维数组元素打乱</span></span><br><span class="line">    randomData();</span><br><span class="line">    <span class="comment">//窗体上组件的绘制</span></span><br><span class="line">    paintView();</span><br><span class="line">    <span class="comment">//设置窗体可见</span></span><br><span class="line">    <span class="built_in">this</span>.setVisible(<span class="literal">true</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="纪录0号图片的索引">2.2.4 纪录0号图片的索引</h4>
<p>为什么要纪录0号图片索引呢？</p>
<ul>
<li>由于将来要进行图片的移动，以实现动漫拼图的实现</li>
<li>而移动的操作，得有一个空白的区别，这里我们采用0号图片表示，需要纪录0号图片的位置，也就是在数组中的索引</li>
</ul>
<p><strong>分析思路：</strong></p>
<p>1：在PictureFrame类中定义两个成员变量用于纪录0号图片的索引</p>
<p>​ private int x0;</p>
<p>​ private int y0;</p>
<p>2：在initData()方法中继续编写代码，在打乱后的数组中找到0号图片的位置</p>
<p>​ 遍历二维数组，得到每一个元素</p>
<p>​ 如果元素为0，则纪录该元素的索引，并结束循环。</p>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义两个int类型的变量，用于纪录0号图片的位置</span></span><br><span class="line"><span class="keyword">private</span> <span class="type">int</span> x0;</span><br><span class="line"><span class="keyword">private</span> <span class="type">int</span> y0;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//二维数组元素打乱</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">randomData</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; datas.length; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; datas[i].length; j++) &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> r.nextInt(datas.length);</span><br><span class="line">            <span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> r.nextInt(datas[i].length);</span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> datas[i][j];</span><br><span class="line">            datas[i][j] = datas[x][y];</span><br><span class="line">            datas[x][y] = temp;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//纪录0号图片的位置</span></span><br><span class="line">    wc:<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; datas.length; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; datas[i].length; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span>(datas[i][j] == <span class="number">0</span>) &#123;</span><br><span class="line">                x0 = i;</span><br><span class="line">                y0 = j;</span><br><span class="line">                <span class="keyword">break</span> wc;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//System.out.println(x0+&quot;,&quot;+y0);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在代码讲解过程中，讲解了一个新知识，就是嵌套循环的退出。给循环起名字就可以实现退出指定的循环。</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="给按钮注册事件">2.2.5 给按钮注册事件</h4>
<p><strong>注意事项：</strong>由于在多个方法中使用同一个按钮，故将按钮的定义放置在成员位置</p>
<p><strong>分析思路：</strong></p>
<p>1：定义方法，用于给按钮添加事件：addButtonEvent()</p>
<p>2：在addButtonEvent()方法中给每一个按钮添加事件，并给出输出语句提示</p>
<p>3：在构造方法中调用addButtonEvent()方法</p>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//给按钮添加事件</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addButtonEvent</span><span class="params">()</span> &#123;</span><br><span class="line">    shangButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;上&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    zuoButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;左&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    xiaButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;下&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    youButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;右&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    qiuzhuButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;求助&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    chongzhiButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;重置&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//无参构造方法</span></span><br><span class="line"><span class="keyword">public</span> <span class="title function_">PictureFrame</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="comment">//用于窗体的基本设置</span></span><br><span class="line">    initFrame();</span><br><span class="line">    <span class="comment">//二维数组元素打乱</span></span><br><span class="line">    randomData();</span><br><span class="line">    <span class="comment">//窗体上组件的绘制</span></span><br><span class="line">    paintView();</span><br><span class="line">    <span class="comment">//给按钮添加事件</span></span><br><span class="line">    addButtonEvent();</span><br><span class="line">    <span class="comment">//设置窗体可见</span></span><br><span class="line">    <span class="built_in">this</span>.setVisible(<span class="literal">true</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="移动业务实现">2.2.6 移动业务实现</h4>
<p><strong>移动原理：</strong>图片的移动，其实就是做元素的交换，也就是二维数组的元素改变了，然后进行图片重绘就可以了</p>
<h5 id="上移业务实现">2.2.6.1 上移业务实现</h5>
<p><strong>分析思路：</strong></p>
<p>1：移动规则：竖的是x，横的是y</p>
<p>​ 空白图片，和下方元素(x0+1)，进行交换</p>
<p>2：把空白图片和下方图片的位置交换</p>
<p>​ datas[x0] [y0] = datas[x0 + 1] [y0];</p>
<p>​ datas[x0 + 1] [y0] = 0;</p>
<p>​ x0 = x0 + 1;</p>
<p>3：编写重绘方法：rePaintView()</p>
<p>​ 先移除，再重新绘制</p>
<p>4：调用重绘方法</p>
<p>5：边界问题处理：当x0=3，不能进行上移动</p>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">shangButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        <span class="comment">//边界处理</span></span><br><span class="line">        <span class="keyword">if</span> (x0 == <span class="number">3</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//位置交换</span></span><br><span class="line">        datas[x0][y0] = datas[x0 + <span class="number">1</span>][y0];</span><br><span class="line">        datas[x0 + <span class="number">1</span>][y0] = <span class="number">0</span>;</span><br><span class="line">        x0 = x0 + <span class="number">1</span>;</span><br><span class="line">        <span class="comment">//重绘方法调用</span></span><br><span class="line">        rePaintView();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//移动的图形重新绘制</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">rePaintView</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="comment">//移除所有</span></span><br><span class="line">    imagePanel.removeAll();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//遍历二维数组，得到每一个图片编号</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; datas.length; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; datas[i].length; j++) &#123;</span><br><span class="line">            <span class="comment">//在遍历的过程中，创建 JLabel 对象，加载图片资源</span></span><br><span class="line">            <span class="type">JLabel</span> <span class="variable">imageLabel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-picture-puzzle\\images\\&quot;</span> + datas[i][j] + <span class="string">&quot;.png&quot;</span>));</span><br><span class="line">            <span class="comment">//调整图片资源的摆放位置</span></span><br><span class="line">            imageLabel.setBounds(j * <span class="number">90</span>, i * <span class="number">90</span>, <span class="number">90</span>, <span class="number">90</span>);</span><br><span class="line">            imagePanel.add(imageLabel);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//重新绘制窗体</span></span><br><span class="line">    imagePanel.repaint();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h5 id="其他移动业务实现">2.2.6.2 其他移动业务实现</h5>
<p><strong>分析思路：</strong></p>
<p>1：左移动</p>
<p>​ 边界：y0=3</p>
<p>​ 移动代码：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">datas[x0][y0] = datas[x0][y0 + 1];</span><br><span class="line"></span><br><span class="line">datas[x0][y0 + 1] = 0;</span><br><span class="line"></span><br><span class="line">y0 = y0 + 1;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>2：下移动</p>
<p>​ 边界：x0=0</p>
<p>​ 移动代码：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">datas[x0][y0] = datas[x0 - 1][y0];</span><br><span class="line"></span><br><span class="line">datas[x0 - 1][y0] = 0;</span><br><span class="line"></span><br><span class="line">x0 = x0 - 1;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>3：右移动</p>
<p>​ 边界：y0=0</p>
<p>​ 移动代码：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">datas[x0][y0] = datas[x0][y0 - 1];</span><br><span class="line"></span><br><span class="line">datas[x0][y0 - 1] = 0;</span><br><span class="line"></span><br><span class="line">y0 = y0 - 1;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">zuoButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (y0 == <span class="number">3</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        datas[x0][y0] = datas[x0][y0 + <span class="number">1</span>];</span><br><span class="line">        datas[x0][y0 + <span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line">        y0 = y0 + <span class="number">1</span>;</span><br><span class="line">        rePaintView();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">xiaButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (x0 == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        datas[x0][y0] = datas[x0 - <span class="number">1</span>][y0];</span><br><span class="line">        datas[x0 - <span class="number">1</span>][y0] = <span class="number">0</span>;</span><br><span class="line">        x0 = x0 - <span class="number">1</span>;</span><br><span class="line">        rePaintView();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">youButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (y0 == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        datas[x0][y0] = datas[x0][y0 - <span class="number">1</span>];</span><br><span class="line">        datas[x0][y0 - <span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line">        y0 = y0 - <span class="number">1</span>;</span><br><span class="line">        rePaintView();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="求助业务实现">2.2.7 求助业务实现</h4>
<figure>
<img data-src="imgs/1636622079054.png" alt="1636622079054">
<figcaption aria-hidden="true">1636622079054</figcaption>
</figure>
<p><strong>注意事项：</strong></p>
<ul>
<li>数组元素应该是1-16，而不是0-15了</li>
<li>按钮也不能在点击了</li>
</ul>
<p><strong>分析思路：</strong></p>
<p>1：定义移动成功的方法：success()</p>
<p>2：在success()方法内部进行如下操作：</p>
<p>​ 修改datas数组的元素为正确的元素值</p>
<p>​ 按钮设置为不可用</p>
<p>3：在重置操作中调用两个方法：</p>
<p>​ 第一个方法：success()</p>
<p>​ 第二个方法：rePaintView()</p>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//移动成功的操作</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">success</span><span class="params">()</span> &#123;</span><br><span class="line">    datas = <span class="keyword">new</span> <span class="title class_">int</span>[][]&#123;</span><br><span class="line">            &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>&#125;,</span><br><span class="line">            &#123;<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>&#125;,</span><br><span class="line">            &#123;<span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>&#125;,</span><br><span class="line">            &#123;<span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>, <span class="number">16</span>&#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    shangButton.setEnabled(<span class="literal">false</span>);</span><br><span class="line">    zuoButton.setEnabled(<span class="literal">false</span>);</span><br><span class="line">    xiaButton.setEnabled(<span class="literal">false</span>);</span><br><span class="line">    youButton.setEnabled(<span class="literal">false</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">qiuzhuButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        success();</span><br><span class="line">        rePaintView();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="移动业务的问题">2.2.8 移动业务的问题</h4>
<p><strong>问题分析：</strong>每次移动之后，都要判断是否移动成功，如果成功了，需要调用成功的方法。</p>
<p>而判断移动是否成功，我们来写方法实现。</p>
<p><strong>分析思路：</strong></p>
<p>1：定义一个方法，用于比较两个数组元素是否相同</p>
<p>2：每次移动完毕，调用该方法：</p>
<p>​ 如果返回值为true，则调用success()方法</p>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义移动成功后的数组</span></span><br><span class="line"><span class="keyword">private</span> <span class="type">int</span>[][] winDatas = &#123;</span><br><span class="line">    &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>&#125;,</span><br><span class="line">    &#123;<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>&#125;,</span><br><span class="line">    &#123;<span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>&#125;,</span><br><span class="line">    &#123;<span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>, <span class="number">0</span>&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//判断移动是否成功</span></span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isSuccess</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; datas.length; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; datas[i].length; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (datas[i][j] != winDatas[i][j]) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">shangButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        <span class="comment">//边界处理</span></span><br><span class="line">        <span class="keyword">if</span> (x0 == <span class="number">3</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//位置交换</span></span><br><span class="line">        datas[x0][y0] = datas[x0 + <span class="number">1</span>][y0];</span><br><span class="line">        datas[x0 + <span class="number">1</span>][y0] = <span class="number">0</span>;</span><br><span class="line">        x0 = x0 + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//判断移动是否成功</span></span><br><span class="line">        <span class="keyword">if</span>(isSuccess()) &#123;</span><br><span class="line">            success();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//调用重绘的方法</span></span><br><span class="line">        rePaintView();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">zuoButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        <span class="comment">//边界处理</span></span><br><span class="line">        <span class="keyword">if</span> (y0 == <span class="number">3</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//位置交换</span></span><br><span class="line">        datas[x0][y0] = datas[x0][y0 + <span class="number">1</span>];</span><br><span class="line">        datas[x0][y0 + <span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line">        y0 = y0 + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//判断移动是否成功</span></span><br><span class="line">        <span class="keyword">if</span>(isSuccess()) &#123;</span><br><span class="line">            success();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//调用重绘的方法</span></span><br><span class="line">        rePaintView();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">xiaButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        <span class="comment">//边界处理</span></span><br><span class="line">        <span class="keyword">if</span> (x0 == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//位置交换</span></span><br><span class="line">        datas[x0][y0] = datas[x0 - <span class="number">1</span>][y0];</span><br><span class="line">        datas[x0 - <span class="number">1</span>][y0] = <span class="number">0</span>;</span><br><span class="line">        x0 = x0 - <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//判断移动是否成功</span></span><br><span class="line">        <span class="keyword">if</span>(isSuccess()) &#123;</span><br><span class="line">            success();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//调用重绘的方法</span></span><br><span class="line">        rePaintView();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">youButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        <span class="comment">//边界处理</span></span><br><span class="line">        <span class="keyword">if</span> (y0 == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//位置交换</span></span><br><span class="line">        datas[x0][y0] = datas[x0][y0 - <span class="number">1</span>];</span><br><span class="line">        datas[x0][y0 - <span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line">        y0 = y0 - <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//判断移动是否成功</span></span><br><span class="line">        <span class="keyword">if</span>(isSuccess()) &#123;</span><br><span class="line">            success();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//调用重绘的方法</span></span><br><span class="line">        rePaintView();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="重置业务实现">2.2.9 重置业务实现</h4>
<figure>
<img data-src="imgs/1636622825802.png" alt="1636622825802">
<figcaption aria-hidden="true">1636622825802</figcaption>
</figure>
<p><strong>分析思路：</strong></p>
<p>1：当重置的时候，需要修改数组为1,2...15,0</p>
<p>2：打乱数组元素</p>
<p>3：重绘面板图</p>
<p>4：设置按钮可用</p>
<p><strong>代码实现：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">chongzhiButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">        datas = <span class="keyword">new</span> <span class="title class_">int</span>[][]&#123;</span><br><span class="line">                &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>&#125;,</span><br><span class="line">                &#123;<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>&#125;,</span><br><span class="line">                &#123;<span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>&#125;,</span><br><span class="line">                &#123;<span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>, <span class="number">0</span>&#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        initData();</span><br><span class="line">        rePaintView();</span><br><span class="line">        shangButton.setEnabled(<span class="literal">true</span>);</span><br><span class="line">        zuoButton.setEnabled(<span class="literal">true</span>);</span><br><span class="line">        xiaButton.setEnabled(<span class="literal">true</span>);</span><br><span class="line">        youButton.setEnabled(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wChat.png" alt="Ethan Liu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Ethan Liu
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/" title="JavaSE基础——Day09 JavaSE基础——综合案例动漫美女拼图">http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE基础/day09动漫美女拼图/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
              <a href="/tags/Java%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> Java基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/" rel="prev" title="JavaSE基础——Day08 JavaSE基础——数组">
                  <i class="fa fa-angle-left"></i> JavaSE基础——Day08 JavaSE基础——数组
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2024/03/25/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day01/day01_JavaSE%E8%BF%9B%E9%98%B6-%E7%BB%A7%E6%89%BF,%E6%8A%BD%E8%B1%A1%E7%B1%BB/" rel="next" title="JavaSE进阶——Day01 继承、抽象类">
                  JavaSE进阶——Day01 继承、抽象类 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="SOHUCS" sid="a368e38d9c433de3d5cc55bd1f413229"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2023-2024 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Is Ethan's</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">113k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">6:51</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni8=" title="在 GitHub 上关注我" aria-label="在 GitHub 上关注我"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.umd.js" integrity="sha256-+2+qOqR8CKoHh/AsVR9k2qaDBKWjYNC2nozhYmv5j9k=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.1.0/medium-zoom.min.js" integrity="sha256-ZgMyDAIYDYGxbcpJcfUnYwNevG/xi9OHKaR/8GK+jWc=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.12/pdfobject.min.js","integrity":"sha256-g2xji1rlE3KsGVClvuxTbcR0Kn2+wtQADSff2Tbb4zA="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>



  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"all","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.3.0/quicklink.umd.js" integrity="sha256-yvJQOINiH9fWemHn0vCA5lsHWJaHs6/ZmO+1Ft04SvM=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":true,"archive":true,"delay":true,"timeout":3000,"priority":true,"url":"http://ethanliu6.github.io/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script class="next-config" data-name="changyan" type="application/json">{"enable":true,"appid":"cyxf6tytI","appkey":"90987f30c4b5248baad5a04c7a6abfe1","count":true}</script>
<script src="/js/third-party/comments/changyan.js"></script>

</body>
</html>
